
$(document).ready(function () {
    let obj=new Object();
    obj.m="list";
    ////初始化商品类别列表
    $.ajax({
        async:true,
        type:"GET",
        url:basePath2+"/api/category",
        dataType:"json",
        data:obj,
        success:function (result) {
           // console.log(result);
            initCategoryList(result);
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
    ///初始化新品商品列表
    let merchantObj=new Object();
    merchantObj.m="getMerchantListBySpecial";
    merchantObj.special=0;
    $.ajax({
        async:true,
        type:"POST",
        url:basePath2+"/api/merchandise",
        dataType:"json",
        data:merchantObj,
        success:function (result) {
        //    console.log(result);
            initNewMerchant(result);
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });

    ///初始化特价商品列表
    let specialMerObj=new Object();
    specialMerObj.m="getMerchantListBySpecial";
    specialMerObj.special=1;
    $.ajax({
        async:true,
        type:"POST",
        url:basePath2+"/api/merchandise",
        dataType:"json",
        data:specialMerObj,
        success:function (result) {
            // console.log(result);
            initSpecialMerchant(result);
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });


    //绑定登录按钮事件
    $(".btnLogin").click(login);
    //注册按钮 测试获得session信息
    $(".btnRegister").click(register);

    //初始化登录页面若session中存在登录信息 那么欢迎你 显示一些其他信息

    //若session中不存在登录信息  显示登录页面
    initLoginView();
//对特价商品的更多操作
    $("#moreSpecial").click(function () {
        location.href="/html/merchandiseList.html?special=1"
    });
    //对新品商品的更多操作
    $("#moreNew").click(function () {
        location.href="/html/merchandiseList.html?special=0"
    });


});

/**
 *
 */
function initLoginView() {
    let obj=new Object();
    obj.m="getLoginMember";
    $.ajax({
        async:true,
        // Ajax请求设置xhrFields的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        type:"POST",
        url:basePath2+"/api/member",
        dataType:"json",
            data:obj,
        success:function (result) {
                console.log(result);
                makeLoginView(result);
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}

/**
 * 展示登录窗口信息
 *
 * @param result
 */
function makeLoginView(result) {
    //存在登录信息
    if(result.code==200){
        let member=result.data;
        $(".loginWrop").html("");
        let txt="";
        txt=txt+"<br>";
        txt=txt+"<span style='font-size=7px; color: green'>欢迎"+member.memberName+"</span>";
        txt=txt+"<br>";
        txt=txt+"<span style='font-size=7px; color: green'>会员级别："+member.leverName+"</span>";
        txt=txt+"<p align='center'><button style=\"width: 100px;background-color: #4380cc;border-radius: 10px;color: white;border: none\" onclick='memberExit()'>安全退出</button></p>"
       $(".loginWrop").html(txt);
    }
}


/**
 * 安全退出
 */
function memberExit() {
    let obj=new Object();
    obj.m="exit";
    $.ajax({
        async:true,
        // Ajax请求设置xhrFields的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        type:"POST",
        url:basePath2+"/api/member",
        dataType:"json",
        data:obj,
        success:function (result) {
           location.href="/index.html";
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });

}

/***
 *注册 测试
 */
function register() {
    let obj=new Object();
    obj.m="getLoginMember";
    $.ajax({
        async:true,
        // Ajax请求设置xhrFields的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        type:"POST",
        url:basePath2+"/api/member",
        dataType:"json",
        data:obj,
        success:function (result) {
            console.log(result);
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}

/**
 * 登录
 */
function login(){
    //拿到登录名
    let loginName=$("#loginName").val();
    //拿到密码
    let loginPwd=$("#loginPwd").val();

    let obj=new Object();
    obj.m="login";
    obj.loginName=loginName;
    obj.loginPwd=loginPwd;
    //Ajax请求
    $.ajax({
        async:true,
        // Ajax请求设置xhrFields的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        type:"POST",
        url:basePath2+"/api/member",
        dataType:"json",
        data:obj,
        success:function (result) {
            console.log(result);
            loginSuccess(result);
        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}

/**
 * 登陆成功
 */
function loginSuccess(result) {
    if(result.code==200){
        console.log(result.msg);//成功
        makeLoginView(result)

    }else{
        console.log(result.msg);//失败
        $("#errorLogin").show();//显示
        // 输入框 用户名获得焦点
        $("#loginName").focus();
    }
}


/**
 * 获得商品详情
 */
function getMerchantDetail(event) {
let id=event.data;
location.href="/html/merchandiseDetail.html?id="+id;
}

/**
 * 将商品添加入购物车
 */
function addshopCart(event) {
    let id=event.data;
   /* //location.href="/html/merchandiseDetail.html?id="+id;*/
    //处理业务
    let obj=new Object();
    obj.m="add";
    obj.merId=id;
    $.ajax({
        async:true,
        // Ajax请求设置xhrFields的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        type:"POST",
        url:basePath2+"/api/auth/shopCar",
        dataType:"json",
        data:obj,
        success:function (result) {
            console.log(result);
            //跳转
            if(result.code==505||result.code==506){
                //跳转到登录页面
                location.href="/html/notLoginView.html";
            }else if(result.code==200){
                //登录成功的话，就跳
             location.href="/html/shopCarList.html"
            }

        },
        error:function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}


/**
     <div class="merchant">
     <img  class="mearchImg" src="/resourse/Picture/8768781-1_b.jpg">
     <br/>
     <a>管理是什么</a>
     <br/>
    <span> 市场价：56￥</span>
     <br/>
    <span> 特价：50.0￥<span>
     <br/>
     <img src="/resourse/images/icon_car.gif">
     <img src="/resourse/images/icon_buy.gif">
     </div>
 * @param result
 */
//初始化新品商品列表
function initNewMerchant(result) {
$("#newsMerchant").html("");
    $.each(result,function (index,element) {
    let  div=$("<div></div>");
    div.addClass("merchant");
    let img1=$("<img>");
    img1.attr("src","/resourse"+element.picture);
    img1.addClass("mearchImg");
    let br=$("<br>");
    let a=$("<a></a>").text(element.merName);
    a.attr("href","#"+element.id);
    let br2=$("<br>");
    let span1=$("<span></span>").text("市场价：￥"+element.price);
    let br3=$("<br>");
    let img2=$("<img>").attr("src","/resourse/images/icon_car.gif");
    img2.bind("click",element.id,getMerchantDetail);

    let img3=$("<img>").attr("src","/resourse/images/icon_buy.gif");
        img3.bind("click",element.id,addshopCart);
    //f放入div
        div.append(img1,br,a,br2,span1,br3,img2,img3);
        $("#newsMerchant").append(div);
    }) ;
}

/**
 * <div class="merchant">
 <img  class="mearchImg" src="/resourse/Picture/8768781-1_b.jpg">
 <br/>
 <a>管理是什么</a>
 <br/>
 市场价：56￥
 <br/>
 特价：50.0￥
 <br/>
 <img src="/resourse/images/icon_car.gif">
 <img src="/resourse/images/icon_buy.gif">
 </div>
 */
//初始化特价商品列表‘
function initSpecialMerchant(result) {
    $("#specialMerchandise").html("");
    $.each(result,function (index,element) {
        let  div=$("<div></div>");
        div.addClass("merchant");
        let img1=$("<img>");
        img1.attr("src","/resourse"+element.picture);
        img1.addClass("mearchImg");
        let br=$("<br>");
        let a=$("<a></a>").text(element.merName);
        a.attr("href","#"+element.id);
        let br2=$("<br>");
        let span1=$("<span></span>").text("市场价：￥"+element.price);
        let br3=$("<br>");
        let span2=$("<span></span>").text("特价：￥"+element.sprice);
        let br4=$("<br>");
        let img2=$("<img>").attr("src","/resourse/images/icon_car.gif");
        //添加点击事件
        img2.bind("click",element.id,getMerchantDetail)
        let img3=$("<img>").attr("src","/resourse/images/icon_buy.gif");
        img3.bind("click",element.id,addshopCart);
        //f放入div
        div.append(img1,br,a,br2,span1,br3,span2,br4,img2,img3);
        $("#specialMerchandise").append(div);
    }) ;
}


//初始化商品类别列表
function initCategoryList(result) {
    $(".categoryUI").html("");//清空值
    //遍历json数组‘
    $.each(result,function (index,element) {
        let li=$("<li></li>");
        let a=$("<a></a>").text(element.cateName);
        a.attr("href","#"+element.id);
        li.append(a);
        //li.attr("value",element.id);
        $(".categoryUI").append(li);
    })

}